package com.sweet.baolei.controller;

import com.sweet.baolei.component.HtmlToPdfComponent;
import com.sweet.baolei.domain.User;
import com.sweet.baolei.service.AdminService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.FileCopyUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.ServletInputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.*;
import java.util.Map;
import java.util.Set;

/**
 * Created by jinxin on 2017/8/18.
 */
@RestController
@RequestMapping("/test")
public class HelloWorldController {

    @Autowired
    private AdminService adminService;

    @Autowired
    private HtmlToPdfComponent htmlToPdfComponent;

    @RequestMapping("a")
    public String sayHello() {
        return "Hello,哈哈哈哈 hehhehehehhehehehehehheheWorld!";
    }

     @RequestMapping("/search")
    public User select(Integer id) {
        return adminService.select(id);
    }


    @RequestMapping("/testpdf")
    public byte[] pdf(@RequestParam("url") String url,HttpServletResponse response) throws IOException {
        File convert = htmlToPdfComponent.convert(url, "hahahha.pdf");
        response.addHeader("Content-Disposition", "inline;filename=\"resume"+""+".pdf"+"\"");
        byte[] bytes= FileCopyUtils.copyToByteArray(convert);
        return bytes;
    }

    @RequestMapping("/testmq")
    public void testmq(HttpServletRequest request, HttpServletResponse response) throws IOException {
        String event_params = request.getParameter("event_params");
//        ServletInputStream inputStream = request.getInputStream();
//        String s;
//        BufferedReader in = new BufferedReader(new InputStreamReader(inputStream, "UTF-8"));
//        StringBuilder sb = new StringBuilder();
//        while ((s = in.readLine()) != null) {
//            sb.append(s);
//        }
//        Set<Map.Entry<String, String[]>> entries = parameterMap.entrySet();
//        for (Map.Entry<String, String[]> entry : entries) {
//            System.out.println("key:"+entry.getKey());
//            System.out.println("value:"+entry.getValue()[0]);
//        }
        System.out.println(event_params);
        System.out.println("系统时间："+System.currentTimeMillis()/1000);

//        String result ="SUCCESS";
//        BufferedOutputStream out = new BufferedOutputStream(
//                response.getOutputStream());
//        out.write(result.getBytes());
//        out.flush();
//        out.close();
    }


    @RequestMapping("/testThread")
    public void testThread(String no , String key)  {

        if("1".equals(no)){
            adminService.update1(key);
        }

        if("2".equals(no)){
            adminService.update2(key);
        }


    }


}
